package frontend.pages.reports.manning;

import backend.model.admin.Foreman;
import backend.service.admin.ForemanService;
import frontend.components.form.ValidatableForm;
import frontend.core.TimeTrackingWebApplication;
import frontend.model.reports.ManningReportUIModel;
import frontend.components.base.ComponentFactory;
import org.apache.log4j.Logger;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.List;

public class ManningReportForm extends ValidatableForm<ManningReportUIModel> {
    private static Logger logger = Logger.getLogger(ManningReportForm.class);

    public static final String RESOURCE_KEY_FIELD_LABEL_REPORT_DATE = "pages.manningReport.form.reportDate";

    public ManningReportForm(String id, ManningReportUIModel uiModel) {
        super(id, uiModel);
    }

    @Override
    protected void constructFormContent() {
        Panel reportDate = ComponentFactory.createDatePickerComponent("date", RESOURCE_KEY_FIELD_LABEL_REPORT_DATE, uiModel);
        WebMarkupContainer foremenComponent = ComponentFactory.createPaletteComponent("foremen", uiModel, getAvailableForemen(), 10, false);
        AjaxButton button = createDownloadButton();

        add(reportDate, foremenComponent, button);
    }

    private AjaxButton createDownloadButton() {
        AjaxButton showWorkLogButton = new AjaxButton("downloadButton", this) {

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                // do something
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                logger.error("Error occurred while submitting form.");
            }
        };
        return showWorkLogButton;
    }

    private List<Foreman> getAvailableForemen() {
        ForemanService foremanService = TimeTrackingWebApplication.getService(ForemanService.class);
        return foremanService.findAllActiveForemen();
    }

    @Override
    protected void doFormValidation() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
